#include <stdio.h>
#include <stdlib.h>

typedef struct DNode{
    char data;
    int freq;
    struct DNode* prev;
    struct DNode* next;
    
}DNode , *PDNode;

PDNode head;

void swap (PDNode p)
{
    char temp_data;
    int temp_freq;

    PDNode pre = p->prev;

    while (pre->prev != NULL &&pre->freq < p->freq )
    {
        temp_data = p->data;
        temp_freq = p->freq;

        p->data = pre->data;
        p->freq = pre->freq;

        pre->data = temp_data;
        pre->freq = temp_freq;

        p = pre;
        pre = pre->prev;
    }

}

void Search (char tem)
{
    PDNode ahead = head;

    while (ahead != NULL)
    {
        if (tem == ahead->data)
        {
            ahead->freq++;
            swap(ahead);
        }
        ahead = ahead->next;
    }
}

void Insert_tail (char val)
{
    PDNode ahead = head;
    PDNode temp;
    temp = (PDNode)malloc(sizeof(DNode));
    temp->data = val;
    temp->next = NULL;
    temp->prev = NULL;
    temp->freq = 0;
    if(ahead == NULL)
    {
        head = temp;
        return;
    }
    while (ahead->next != NULL)
    {
        ahead = ahead->next;
    }
    ahead->next = temp;
    temp->prev = ahead;
}

void Print ()
{
    PDNode temp = head;
    temp = temp->next;
    printf ("\n");
    while (temp != NULL)
    {   
        if (temp->next != NULL)
        printf ("%c ", temp->data);
        else
        printf ("%c", temp->data);

        temp = temp->next;
    }
}

int main ()
{
    head = (PDNode)malloc(sizeof(DNode));
    

    int m , n;
    scanf ("%d%d" , &m , &n);
    setbuf(stdin, NULL);

    char val;
    char word , word1;
    for (int i = 0 ; i < m ; i++)
    {   
        scanf (" %c" , &val);
        setbuf(stdin, NULL);
        Insert_tail(val);
    }//insert
    word = getchar();
    for (int j = 0 ; j < n ; j++)
    {
        scanf (" %c" , &word1);
        if (word1 != '\n')
        {
            Search(word1);
        }  
    }
    
    Print();
    return 0;
}